package com.appkudos.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appkudos.controller.UserController;
import com.appkudos.model.Group;
import com.appkudos.model.User;
import com.appkudos.view.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class UserGroupActivity extends Activity implements ViewBinder{
    ListView lv;
    Button joinGroupButton;
    Button createGroupButton;
	private Activity PARENTACTIVITY; 
	User user;
	User friend;
	UserController uc;
	ArrayList<Group> groups;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_group_list);
        PARENTACTIVITY = this;
        
	    // get user info
	    Intent launchIntent = getIntent();
	    Bundle extras = launchIntent.getExtras();
	    uc = new UserController();
	    user = uc.getCurrentUser(extras);
	    friend = uc.getFriend(extras);
        
  	    // get listview from layout
  	    lv=(ListView)findViewById(R.id.my_group_list);
  	    
  	    // get the groups this user belongs
	    // get friends for this user
  	    if(friend == null){
  	    	groups = (ArrayList<Group>) uc.getGroupsOfUser(user.getUsername());
  	    }else{
  	    	groups = (ArrayList<Group>) uc.getGroupsOfUser(friend.getUsername());
  	    }
  	   
  	    // display friends
  	    displayMatchedGroups(groups);
  	  
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		      Group grp = groups.get(position);
		      // When a user is clicked, go to the user's profile
		      Toast.makeText(getApplicationContext(), grp.getGroupName()+" is clicked",Toast.LENGTH_SHORT).show();
		     
		      Intent intent = new Intent(PARENTACTIVITY, AppKudos.class);
  		      intent = uc.passCurrentUser(intent, user);
              intent = uc.passCurrentGroup(intent, grp);
  		      startActivity(intent);
		    }
		});
		
        // enable filter on text
  	    lv.setTextFilterEnabled(true);
  	    
  	    // open a new page when add friend is clicked
  	    joinGroupButton = (Button) this.findViewById(R.id.b_join_group);
        joinGroupButton.setOnClickListener(new OnClickListener() {
        
        	@Override
             public void onClick(View v) {     

                 Intent intent = new Intent(PARENTACTIVITY, JoinGroupActivity.class);
                 intent = uc.passCurrentUser(intent, user);
         		 startActivity(intent);
            	   Toast.makeText(getApplicationContext(), "join group is clicked",
            			      Toast.LENGTH_SHORT).show();
             }
           });  	    // open a new page when add friend is clicked
  	   
         createGroupButton = (Button) this.findViewById(R.id.b_create_group);
         createGroupButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {     
            	   Toast.makeText(getApplicationContext(), "create group is clicked",Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(PARENTACTIVITY, CreateGroupActivity.class);
                   intent = uc.passCurrentUser(intent, user);
           		   startActivity(intent);
             }
           });    
   }
    
   // helper method to display matched users
	private void displayMatchedGroups(List list){
	  	Map<String, Group> itemData;
	  	Group matched;
	  	List<Map<String, Group>> listData = new ArrayList<Map<String, Group>>();
	  	
	  	// put the friends into a hashmap (used for input by simple adapter)
	  	for(int j=0; j<list.size();j++){
	  		itemData = new HashMap<String, Group>(1);
	  		matched = (Group) list.get(j);
	  		itemData.put("single_list_item", matched);
	  		listData.add(itemData);
	  	}    
	
	  	// bind simple adapter to the list view
	      SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.single_text_item,
	              new String[] {
	                  "single_list_item"
	              }, new int[] {
	                  R.id.single_list_item
	              });
	      
	      // bind viewBinder to adapter and set adapter to listview
	      simpleAdapter.setViewBinder(this);
	      lv.setAdapter(simpleAdapter);
	  }

	 @Override
	  public boolean setViewValue(View view, Object data, String stringRepresetation) {  	
		  	  // get data for the row
		  	  Group matched = (Group) data;
		      TextView tv = (TextView) view;
		      
		      // bind the matched user to textView
		      tv.setText(matched.getGroupName());

		      return true;
	  } 
}